//
//  RemondTableViewCell.m
//  VEMoneyPorject
//
//  Created by BlueWind on 2018/9/25.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "RemondTableViewCell.h"

@interface RemondTableViewCell()

@property (nonatomic, strong)UIView *imageContentView; /**<图片 view*/

@property (nonatomic, strong)UIButton *whichSocietyButton; /**<属于哪一个社区*/

@property (nonatomic, strong)UIView *buttonContentView; /**<点赞，评论，转发按钮 view*/

@property (nonatomic, strong)UILabel *likeLabel; /**<点赞文字*/


@end

@implementation RemondTableViewCell

/** 返回循环利用的cell */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self= [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self loadDefaultSetting];
    }
    return self;
}

/** 加载默认UI元素和需要的数据 */
- (void)loadDefaultSetting {
    
    [self.summaryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(GAP8);
        make.left.equalTo(self.contentView).offset(GAP15);
        make.right.equalTo(self.contentView).offset(-GAP15);
    }];
    
    [self.lookAll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.summaryLabel.mas_bottom);
        make.left.equalTo(self.summaryLabel);
        make.height.mas_equalTo(30);
    }];
    
    [self.imageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lookAll.mas_bottom).offset(GAP8);
        make.left.equalTo(self.summaryLabel);
        make.right.equalTo(self.summaryLabel);
        make.height.mas_equalTo((SCREEN_WIDTH - 2 *(GAP15 + GAP8))/3 + 16);
    }];
    
    [self.whichSocietyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageContentView.mas_bottom).offset(GAP8);
        make.left.equalTo(self.imageContentView);
//        make.height.mas_equalTo(24);
    }];
    
    [self.buttonContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.whichSocietyButton.mas_bottom).offset(GAP8);
        make.left.equalTo(self.summaryLabel);
        make.height.mas_offset(30);
        make.width.mas_equalTo(120);
    }];
    
    [self.likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.buttonContentView.mas_bottom).offset(GAP8);
        make.left.equalTo(self.buttonContentView);
        make.right.equalTo(self.imageContentView);
    }];
    
}

- (void)setModel:(VERemondModel *)model {
    
    _model = model;
    
    CGFloat h = [model.DYNAMICTEXT heigthWithWeight:(SCREEN_WIDTH - 30) andFont:16 andLineSpacing:2 andWordSpacing:1];
    
    if (h > 100) {
        
        if (_isDetail == NO) {
            _summaryLabel.numberOfLines = 4;
            self.lookAll.hidden = NO;
            [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.lookAll.mas_bottom).offset(GAP8);
                make.left.equalTo(self.summaryLabel);
                make.right.equalTo(self.summaryLabel);
                make.height.mas_equalTo((SCREEN_WIDTH - 2 *(GAP15 + GAP8))/3 + 16);
            }];
        } else {
            _summaryLabel.numberOfLines = 0;

            self.lookAll.hidden = YES;
            [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.summaryLabel.mas_bottom).offset(GAP8);
                make.left.equalTo(self.summaryLabel);
                make.right.equalTo(self.summaryLabel);
                make.height.mas_equalTo((SCREEN_WIDTH - 2 *(GAP15 + GAP8))/3 + 16);
            }];
        }
        
    } else {
        if (_isDetail == NO) {
            _summaryLabel.numberOfLines = 4;

            self.lookAll.hidden = YES;
            [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.summaryLabel.mas_bottom).offset(GAP8);
                make.left.equalTo(self.summaryLabel);
                make.right.equalTo(self.summaryLabel);
                make.height.mas_equalTo((SCREEN_WIDTH - 2 *(GAP15 + GAP8))/3 + 16);
            }];
        } else {
            _summaryLabel.numberOfLines = 0;
            self.lookAll.hidden = YES;
            [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.summaryLabel.mas_bottom).offset(GAP8);
                make.left.equalTo(self.summaryLabel);
                make.right.equalTo(self.summaryLabel);
                make.height.mas_equalTo((SCREEN_WIDTH - 2 *(GAP15 + GAP8))/3 + 16);
            }];
        }
        

    }
    self.summaryLabel.text = model.DYNAMICTEXT;
    
    if (model.DYNAMICPHOTO.length == 0) {
        self.imageContentView.hidden = YES;
        if (self.lookAll.hidden == NO) {
            [self.whichSocietyButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.lookAll.mas_bottom).offset(GAP8);
                make.left.equalTo(self.summaryLabel);
                make.height.mas_equalTo(30);
            }];
        } else {
            [self.whichSocietyButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.summaryLabel.mas_bottom).offset(GAP8);
                make.left.equalTo(self.summaryLabel);
                make.height.mas_equalTo(30);
            }];
        }

    } else {
        self.imageContentView.hidden = NO;
        NSArray *imageArray = [model.DYNAMICPHOTO componentsSeparatedByString:@","];

        
        double row = ceil(imageArray.count/3.0);
        
        
        CGFloat h = ((SCREEN_WIDTH - 2 *(GAP15 + GAP8))/3 + 8 ) * row;
        
        if (self.lookAll.hidden == NO) {
            [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.lookAll.mas_bottom).offset(GAP8);
                make.left.equalTo(self.summaryLabel);
                make.right.equalTo(self.summaryLabel);
                make.height.mas_equalTo(h);
            }];
        } else {
            [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.summaryLabel.mas_bottom).offset(GAP8);
                make.left.equalTo(self.summaryLabel);
                make.right.equalTo(self.summaryLabel);
                make.height.mas_equalTo(h);
            }];
        }

        [self.whichSocietyButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageContentView.mas_bottom).offset(GAP8);
            make.left.equalTo(self.summaryLabel);
            make.height.mas_equalTo(30);
        }];
        
        [self.imageContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        CGFloat PIC_WIDTH = (SCREEN_WIDTH-6 *GAP8)/3;
        CGFloat PIC_HEIGHT = (SCREEN_WIDTH-6 *GAP8)/3;
        NSInteger COL_COUNT = 3;
        // 间距
        CGFloat margin = 8;
        
        for (int i = 0; i < imageArray.count; i++) {
            if (i == 9) {
                return;
            }
            // 图片所在行
            NSInteger row = i / COL_COUNT;
            // 图片所在列
            NSInteger col = i % COL_COUNT;
            // PointX
            CGFloat picX = (PIC_WIDTH + margin) * col;
            // PointY
            CGFloat picY = (PIC_HEIGHT + margin) * row;
            
            UIView * bgView = [[UIView alloc] init];
            bgView.frame = CGRectMake(picX, picY, PIC_WIDTH, PIC_HEIGHT);
            [self.imageContentView addSubview:bgView];
            
            UIImageView * imageView = [[UIImageView alloc] init];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            imageView.tag = 20 + i;
            imageView.frame = CGRectMake(0, 0, PIC_WIDTH, PIC_HEIGHT);
            [imageView setImageWithUrlString:imageArray[i] palceHolderImage:[UIImage imageNamed:@"First_PlaceHolder"]];
            [bgView addSubview:imageView];
            
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAddImageView:)]];
        }
    }
    [self.whichSocietyButton setTitle:model.COMMUNITYNAME forState:(UIControlStateNormal)];
    
    
    if (_isDetail == YES) {
        self.whichSocietyButton.hidden = YES;
        
        if (self.imageContentView.hidden == YES) {
            [self.buttonContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.summaryLabel.mas_bottom).offset(GAP8);
                make.left.equalTo(self.summaryLabel);
                make.height.mas_offset(30);
                make.width.mas_equalTo(120);
            }];
        } else {
            
        }
        
        
    } else {
        self.whichSocietyButton.hidden = NO;
        [self.buttonContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.whichSocietyButton.mas_bottom).offset(GAP8);
            make.left.equalTo(self.summaryLabel);
            make.height.mas_offset(30);
            make.width.mas_equalTo(120);
        }];
    }
    
    if (model.ZANNAME.length == 0) {
        self.likeLabel.hidden = YES;
    } else {
        self.likeLabel.hidden = NO;
        //创建富文本
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@", model.ZANNAME]];
        //NSTextAttachment可以将要插入的图片作为特殊字符处理
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        //定义图片内容及位置和大小
        attch.image = [UIImage imageNamed:@"超级赞"];
        attch.bounds = CGRectMake(0, -3, 16, 16);
        //创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
        //将图片放在第一位
        [attri insertAttributedString:string atIndex:0];
        //用label的attributedText属性来使用富文本
        self.likeLabel.attributedText = attri;
    }
    
    
}


- (void)caozuo:(UIButton *)button {
    if (button.tag == 10) { //点赞
        if (_clickLike) {
            _clickLike();
        }
  
    } else if (button.tag == 11) {
        if (_sendMessage) {
            _sendMessage();
        }
        
    } else if (button.tag == 12) {
        
        if (_share) {
            _share();
        }
        
    }
}

- (void)enter {
    if (_enterAssion) {
        _enterAssion();
    }
}


- (UIView *)imageContentView {
    if (!_imageContentView) {
        _imageContentView = [[UIView alloc] init];
        [self.contentView addSubview:_imageContentView];
    }
    return _imageContentView;
}

- (UIButton *)whichSocietyButton {
    if (!_whichSocietyButton) {
        _whichSocietyButton = [[UIButton alloc] init];
        [self.contentView addSubview:_whichSocietyButton];
        [_whichSocietyButton setContentEdgeInsets:UIEdgeInsetsMake(2, 8, 2, 8)];
        [_whichSocietyButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_whichSocietyButton layerBorderWithWidth:0.5 borderColor:[UIColor orangeColor]];
        [_whichSocietyButton setLayerCornerRadius:5];
        [_whichSocietyButton addTarget:self action:@selector(enter) forControlEvents:(UIControlEventTouchUpInside)];
        _whichSocietyButton.titleLabel.font = kFont(12);
        [_whichSocietyButton setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    }
    return _whichSocietyButton;
}

- (UILabel *)summaryLabel {
    if (!_summaryLabel) {
        _summaryLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_summaryLabel];
        _summaryLabel.numberOfLines = 4;
        _summaryLabel.font = kFont(16);
    }
    return _summaryLabel;
}

- (UIButton *)lookAll {
    if (!_lookAll) {
        _lookAll = [[UIButton alloc] init];
        [self.contentView addSubview:_lookAll];
        [_lookAll setTitle:@"查看全文" forState:(UIControlStateNormal)];
        [_lookAll setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
        _lookAll.titleLabel.font = kFont(14);
    }
    return _lookAll;
}

- (UIView *)buttonContentView {
    if (!_buttonContentView) {
        _buttonContentView = [[UIView alloc] init];
        [self.contentView addSubview:_buttonContentView];
        NSArray *imageArray = @[@"点赞",@"留言",@"分享"];
        
        for (NSInteger index = 0; index < 3; index ++) {
            UIButton *button = [UIButton new];
            [_buttonContentView addSubview:button];
            button.tag = 10 + index;
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self->_buttonContentView).offset(index * 30 + GAP15 * (index + 1));
                make.centerY.equalTo(self->_buttonContentView);
                make.size.mas_equalTo(CGSizeMake(24, 24));
            }];
            [button setImage:[UIImage imageNamed:imageArray[index]] forState:(UIControlStateNormal)];
            [button addTarget:self action:@selector(caozuo:) forControlEvents:(UIControlEventTouchUpInside)];
        }
    }
    return _buttonContentView;
}

- (UILabel *)likeLabel {
    if (!_likeLabel) {
        _likeLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_likeLabel];
        _likeLabel.textColor = [UIColor orangeColor];
        _likeLabel.numberOfLines = 0;
        _likeLabel.font = kFont(14);
    }
    return _likeLabel;
}

- (void)clickAddImageView:(UITapGestureRecognizer *)tap {
    
    NSArray *imageArray = [self.model.DYNAMICPHOTO componentsSeparatedByString:@","];
    if (_clickImage) {
        _clickImage(imageArray,(UIImageView *)tap.view);
    }
    
}

@end
